<?php

use Recetas\Traits\UtilTrait;

class Recipe extends Eloquent {
	
	use UtilTrait;
	
	/**
	 * Relations
	 */
	public function user(){
		return $this->belongsTo( 'User' );
	}

	public function steps(){
		return $this->hasMany( 'RecipesSteps' );
	}
	
	public function comments(){
		return $this->hasMany( 'RecipesComments' );
	}
	
	public function commentsCount(){
		return $this->hasMany( 'RecipesComments' )->select( array( DB::raw( 'count(*) as count' ), 'recipe_id' ) )
			->groupBy( 'recipe_id' );
	}
	
	public function commentsNoAnswersCount(){
		return $this->hasMany( 'RecipesComments' )->select( array( DB::raw( 'count(*) as count' ), 'recipe_id' ) )
			->whereNull( 'parent_comment' )->groupBy( 'recipe_id' );
	}
	 
	public function commentsPagination(){
		return $this->hasMany( 'RecipesComments' )->whereNull( 'parent_comment' )->orderBy( 'created_at', 'desc' )
			->take( RecipesComments::RECORDS_PER_PAGE );
	}
	
	public function likesDislikes(){
		return $this->hasMany( 'RecipesLikesDislikes' );
	}
}
